package cn.highset.modules.base.controller.manage;

import cn.highset.common.utils.PageUtil;
import cn.highset.common.utils.ResultUtil;
import cn.highset.common.vo.PageVo;
import cn.highset.common.vo.Result;
import cn.highset.common.vo.SearchVo;
import cn.highset.modules.base.entity.Vote;
import cn.highset.modules.base.factory.VoteFactory;
import cn.highset.modules.base.service.VoteService;
import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @author Victor
 */
@Slf4j
@RestController
@Api(description = "投票活动管理接口")
@RequestMapping("/highset/vote")
@Transactional
public class VoteController {

    @Autowired
    private VoteService voteService;


    @PostMapping(value = "/getByCondition")
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<Vote>> getByCondition(@ModelAttribute Vote vote,
                                             @ModelAttribute SearchVo searchVo,
                                             @ModelAttribute PageVo pageVo) {

        Page<Vote> page = voteService.findByCondition(vote, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<Vote>>().setData(page);
    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation(value = "添加投票活动")
    public Result<Object> add(@ModelAttribute Vote o) {

        if (o.getTimeStart() == null || o.getTimeEnd() == null || StrUtil.hasBlank(o.getVoteName())) {
            return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
        }

        Vote bean = voteService.save(o);
        if (bean == null) {
            return new ResultUtil<Object>().setErrorMsg("添加失败");
        }
        return new ResultUtil<Object>().setSuccessMsg("添加成功");
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ApiOperation(value = "修改投票活动", notes = "需要通过id获取原投票活动信息")
    public Result<Object> edit(@ModelAttribute Vote o) {

        if (o.getTimeStart() == null || o.getTimeEnd() == null || StrUtil.hasBlank(o.getId(), o.getVoteName())) {
            return new ResultUtil<Object>().setErrorMsg("缺少必需表单字段");
        }
        Vote old = voteService.get(o.getId());

        Vote bean = voteService.update(VoteFactory.edit(o, old));

        if (bean == null) {
            return new ResultUtil<Object>().setErrorMsg("修改失败");
        }
        return new ResultUtil<Object>().setSuccessMsg("修改成功");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过ids删除", notes = "逻辑删除")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(paramType = "path", name = "ids", value = "投票活动ID数组", dataType = "String", required = true, allowMultiple = true),
    })
    public Result<Object> delAllByIds(@PathVariable String[] ids) {
        for (String id : ids) {
            voteService.delete(id);
        }

        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }


}
